package com.experiment08;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class IoTest {
    public static void main(String[] args)  {
        String fileName = "C:/example/from.txt";
        String str =
                "白日依山尽\r\n" +
                        "黄河入海流\r\n" +
                        "欲穷千里目\r\n" +
                        "更上一层楼\r\n";
        String toFile = "C:/example/to.txt";
        String toFile2 = "C:/example/nio/to.txt";
        try {
            System.out.println("----- 创建文件 ------");
            createFile(fileName);
            System.out.println("-----  将字符串写入文件 -------");
            //writeToFile(fileName, str);
            //writeToFile2(fileName, str);
            System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
            //copyByIO(fileName, toFile);
            System.out.println("--------- 基于NIO实现文件的复制 ----------");
            //copyByIO(fileName, toFile2);
            System.out.println("---------- 删除指定文件 -------------");
            //deleteFile(toFile);
            System.out.println("---------- 遍历指定目录文件 -------------");
            //String dir = "C:/example";
        }catch (IOException e){
            System.out.println(e.getMessage());

        }


    }
    private static void createFile(String fileName) throws IOException {
        Path path = Path.of(fileName);
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    }
    private static void writeToFile(String fileName, String content) throws IOException {

        try (FileOutputStream out = new FileOutputStream(fileName)) {
            byte[] buffer = content.getBytes();
            out.write(buffer);
        }
    }
    private static void writeToFile2(String fileName, String content) throws IOException {
        Path path = Path.of(fileName);
        Files.createDirectories(path.getParent());
        Files.createFile(path);
        try(FileOutputStream out = new FileOutputStream(path.toFile())){
            byte[] buffer = content.getBytes();
            out.write(buffer);
        }

    }
    private static void copyByIO(String sourceFile, String targetFile) throws IOException{
        try(FileInputStream in = new FileInputStream(sourceFile);
            FileOutputStream out = new FileOutputStream(targetFile)) {
            byte[] buffer = new byte[10];
            int read = 0;
            int len = 0;
            while((len = in.read(buffer)) != -1){
                out.write(buffer,0,len);
                read++;
            }
            System.out.println(read);

        }


    }
    private static void copyByNIO(String sourceFile, String targetFile) throws IOException {
        Path s = Path.of(sourceFile);
        Path t = Path.of(targetFile);
        Files.createDirectories(s.getParent());
        Files.createDirectories(t.getParent());
        Files.copy(s,t);
    }
    private static void deleteFile(String fileName) throws IOException {
        Path path = Path.of(fileName);
        Files.deleteIfExists(path);

    }
    private static void walkDirectories(String dir) throws IOException {
        Path d = Path.of(dir);
        Files.walk(d).forEach(System.out::println);
    }



}
